<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/10 0010
 * Time: 15:58
 */

namespace app\api\controller;
use app\admin\model\Bmxx;
use app\admin\model\Kczs;
use app\admin\model\Lyb;
use think\Controller;
use app\admin\model\Zp;
use app\admin\model\Kcb;
use app\admin\model\Ksb;
use app\admin\model\Admin;
use think\Db;
use think\Session;
class Home extends Controller
{
 public function index(){
     return $this->fetch();
 }
 public function gsjj(){
     return $this->fetch();
 }

 public function courseList(){
     $param         = input('post.');

     $model         = new Kczs();

     $return        = $model->getCourseList($param);

     response(200 , $return);
 }

 public function kczs(){
     $model=new Kczs();
     $kczss=$model->getKczs();
     return json([ 'code' => 200 , 'data'=> $kczss ]);

 }
    public function wybm(){
        return $this->fetch();

    }
    public function rczp(){
        return $this->fetch();

    }
    public function homerczp(){
        $this->_commonOperation('Zp');
    }

    public function homerStudent(){
        $this->_commonOperation('student');
    }

    public function zxly(){
        $this->_commonOperation('Lyb');

    }
    public function homelyb(){
     $data=  $this->request->param();
     $model=new Lyb();
     $res=$model->save($data);
     if ($res){
         $this->success('留言成功','home/index');
     }else{
         $this->error('留言失败');
     }

    }

    /**
     * 我要报名
     */
    public function homebmxx(){
        $this->_commonOperation('Bmxx');
    }

    public function lxwm(){
        return $this->fetch();

    }
    public function tz(){


    }
    public function mlzj($no){
        //获取kc
        $model=new Kcb();
        $kc= $model->getKc($no);

        //获取ks
        $ks_model=new Ksb();
        $kss= $ks_model->getList();

        return $this->fetch('mlzj',['kc'=>$kc,'kss'=>$kss]);
    }

    public function video($no,$kcid){
     $model=new Ksb();
    $kss= $model->getks($no);
        //获取kc
        $model=new Kcb();
        $kc= $model->getKc2($kcid);

        //获取ks
        $ks_model=new Ksb();
        $ksss= $ks_model->getList();

    return $this->fetch('video',['ksb'=>$kss,'kc'=>$kc,'kss'=>$ksss]);

    }


    public function gerenzhongxin(){
        return $this->fetch();

    }
    public function gerenxinxi(){
        return $this->fetch();

    }
    public function xuexizhoubao(){
        return $this->fetch();

    }
    public function login(){
        return $this->fetch();

    }
    public function dologin($username, $password)
    {
        $user = Db::name('admin')->where('Aname', $username)->find();
        if (empty($user)) {
            $this->error('用户不存在');
        }
        if ($user['Apassword'] == $password) {
            Session::set('username', $username);
            $this->success('登陆成功', 'home/gerenzhongxin');

        } else {
            $this->error('密码错误');
        }
    }
    public function logout()
    {
        session(null);
        $this->success('退出成功', 'home/index');
    }
    public function register()
    {
        return $this->fetch();

    }

    public function doreg()
    {

        $data = $this->request->param();
        $model = new Admin();
        $res = $model->save($data);
        if ($res) {
            $this->success('注册成功', 'home/login');
        } else {
            $this->error('注册失败');
        }
    }

    private function _commonOperation($model){
        $data           = $this->request->param();
        array_shift($data);
        if(empty($data)){
            response(105 , '' , '请求参数不能为空');
        }
        $model          = "\\app\\admin\\model\\" . ucfirst($model);
        $model          = new $model;
        $result         = $model->insertData($data);
        $result         = $result ? '添加成功' : '添加失败';

        response(200 , '' , $result);
    }
}